package com.anolesoft.yr.mbus.crm.service.impl;

import com.anolesoft.yr.mbus.crm.dto.CrmBaseDTO;
import com.anolesoft.yr.mbus.crm.dto.CrmBasePartPriceDTO;
import com.anolesoft.yr.mbus.crm.dto.CrmBasePartPriceQueryDTO;
import com.anolesoft.yr.mbus.crm.entity.CrmBaseDO;
import com.anolesoft.yr.mbus.crm.entity.CrmBasePartPriceDO;
import com.anolesoft.yr.mbus.crm.entity.CrmBasePartPriceQueryDO;
import com.anolesoft.yr.mbus.crm.mapper.CrmBasePartPriceMapper;
import com.anolesoft.yr.mbus.crm.mapper.CrmBasePartPriceQueryMapper;
import com.anolesoft.yr.mbus.crm.service.CrmBasePartPriceService;
import com.anolesoft.yr.mbus.part.dto.PartModelDTO;
import com.anolesoft.yr.mbus.part.entity.PartModelDO;
import com.anolesoft.yr.util.CommonUtils;
import com.anolesoft.yr.util.CustomBeanUtils;
import com.anolesoft.yr.util.PageData;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author zhj
 * @date 2020/8/12 13:41
 */
@Service("crmBasePartPriceServiceImpl")
public class CrmBasePartPriceServiceImpl implements CrmBasePartPriceService {
    @Autowired
    private CrmBasePartPriceMapper crmBasePartPriceMapper;

    @Autowired
    private CrmBasePartPriceQueryMapper crmBasePartPriceQueryMapper;

    @Transactional
    @Override
    public int create(CrmBasePartPriceDTO crmBasePartPriceDTO) {
        return crmBasePartPriceMapper.insert(CrmBasePartPriceDO.builder()
                .contMainUid(crmBasePartPriceDTO.getContMainUid())
                .prodProductUid(crmBasePartPriceDTO.getProdProductUid())
                .preOrderUid(crmBasePartPriceDTO.getPreOrderUid())
                .partModelUid(crmBasePartPriceDTO.getPartModelUid())
                .crmBaseUid(crmBasePartPriceDTO.getCrmBaseUid())
                .factoryCode(crmBasePartPriceDTO.getFactoryCode())
                .offerPerson(crmBasePartPriceDTO.getOfferPerson())
                .objuid(crmBasePartPriceDTO.getObjuid())
                .createTime(crmBasePartPriceDTO.getCreateTime())
                .createUserUid(crmBasePartPriceDTO.getCreateUserUid())
//                .dealPrice(crmBasePartPriceDTO.getDealPrice())
                .deleteFlag(crmBasePartPriceDTO.getDeleteFlag())
                .modifyTime(crmBasePartPriceDTO.getModifyTime())
                .modifyUserUid(crmBasePartPriceDTO.getModifyUserUid())
                .offerPrice(crmBasePartPriceDTO.getOfferPrice())
                .offerTime(crmBasePartPriceDTO.getOfferTime())
                .remark(crmBasePartPriceDTO.getRemark())
                .build());
    }

    @Transactional
    @Override
    public int modify(CrmBasePartPriceDTO crmBasePartPriceDTO) {
        CrmBasePartPriceDO crmBasePartPriceDO = crmBasePartPriceMapper.selectOneById(crmBasePartPriceDTO.getObjuid());
        if(crmBasePartPriceDO == null){
            crmBasePartPriceDO = CrmBasePartPriceDO.builder().build();
        }
        crmBasePartPriceDO.setContMainUid(crmBasePartPriceDTO.getContMainUid());
        crmBasePartPriceDO.setPreOrderUid(crmBasePartPriceDTO.getPreOrderUid());
        crmBasePartPriceDO.setProdProductUid(crmBasePartPriceDTO.getProdProductUid());
        crmBasePartPriceDO.setPartModelUid(crmBasePartPriceDTO.getPartModelUid());
        crmBasePartPriceDO.setCrmBaseUid(crmBasePartPriceDTO.getCrmBaseUid());
        crmBasePartPriceDO.setOfferPerson(crmBasePartPriceDTO.getOfferPerson());
        crmBasePartPriceDO.setObjuid(crmBasePartPriceDTO.getObjuid());
//        crmBasePartPriceDO.setDealPrice(crmBasePartPriceDTO.getDealPrice());
        crmBasePartPriceDO.setOfferPrice(crmBasePartPriceDTO.getOfferPrice());
        crmBasePartPriceDO.setOfferTime(crmBasePartPriceDTO.getOfferTime());
        return crmBasePartPriceMapper.update(crmBasePartPriceDO);
    }

    @Transactional
    @Override
    public int remove(String id) {
        return crmBasePartPriceMapper.delete(id);
    }

    @Override
    public List<CrmBasePartPriceDTO> selectAll() {
        return crmBasePartPriceMapper.selectAll().stream().map(t -> CustomBeanUtils.crmBasePartPriceDO2DTO(t)).collect(Collectors.toList());
    }

//    private CrmBasePartPriceDTO do2dto(CrmBasePartPriceDO t) {
//        if (t == null) {
//            return null;
//        }
//        return CrmBasePartPriceDTO.builder()
//                .objuid(t.getObjuid())
//                .offerPerson(t.getOfferPerson())
//                .crmBaseUid(t.getCrmBaseUid())
//                .partModelUid(t.getPartModelUid())
//                .factoryCode(t.getFactoryCode())
//                .remark(t.getRemark())
//                .offerTime(t.getOfferTime())
//                .offerPrice(t.getOfferPrice())
//                .modifyUserUid(t.getModifyUserUid())
//                .modifyTime(t.getModifyTime())
//                .deleteFlag(t.getDeleteFlag())
//                .dealPrice(t.getDealPrice())
//                .createUserUid(t.getCreateUserUid())
//                .createTime(t.getCreateTime())
//                .build();
//    }

    @Override
    public PageData<List<CrmBasePartPriceDTO>> selectAll(Integer limit, Integer curr) {
        Page<CrmBasePartPriceDO> page = new Page<>(curr, limit);
        List<CrmBasePartPriceDO> crmBasePartPriceDOS = crmBasePartPriceMapper.selectPageAll(page);
        List<CrmBasePartPriceDTO> crmBasePartPriceDTOS = crmBasePartPriceDOS.stream().map(t -> CustomBeanUtils.crmBasePartPriceDO2DTO(t)).collect(Collectors.toList());
        return new PageData<>(((int) page.getTotal()), crmBasePartPriceDTOS);
    }

    @Override
    public Optional<CrmBasePartPriceDTO> selectOneById(String id) {
        CrmBasePartPriceDO crmBasePartPriceDO = crmBasePartPriceMapper.selectOneById(id);
        if (crmBasePartPriceDO != null) {
            return Optional.of(CustomBeanUtils.crmBasePartPriceDO2DTO(crmBasePartPriceDO));
        }
        return Optional.empty();
    }

    @Override
    public List<CrmBasePartPriceDTO> selectList(CrmBasePartPriceDTO crmBasePartPriceDTO) {
        CrmBasePartPriceDO crmBasePartPriceDO = buildCondition(crmBasePartPriceDTO);
        return crmBasePartPriceMapper.selectList(crmBasePartPriceDO).stream().map(t -> CustomBeanUtils.crmBasePartPriceDO2DTO(t)).collect(Collectors.toList());
    }

    private CrmBasePartPriceDO buildCondition(CrmBasePartPriceDTO crmBasePartPriceDTO) {
        return CrmBasePartPriceDO.builder()
                .factoryCode(crmBasePartPriceDTO.getFactoryCode())
                .crmBaseUid(crmBasePartPriceDTO.getCrmBaseUid())
                .partModelUid(crmBasePartPriceDTO.getPartModelUid())
                .offerPerson(crmBasePartPriceDTO.getOfferPerson())
                .objuid(crmBasePartPriceDTO.getObjuid())
                .build();
    }

    @Override
    public PageData<List<CrmBasePartPriceDTO>> selectList(CrmBasePartPriceDTO crmBasePartPriceDTO, Integer limit, Integer curr) {
        Page<CrmBasePartPriceDO> page = new Page<>(curr, limit);
        CrmBasePartPriceDO crmBasePartPriceDO = buildCondition(crmBasePartPriceDTO);
        List<CrmBasePartPriceDO> crmBasePartPriceDOS = crmBasePartPriceMapper.selectPageList(crmBasePartPriceDO, page);
        List<CrmBasePartPriceDTO> crmBasePartPriceDTOS = crmBasePartPriceDOS.stream().map(t -> CustomBeanUtils.crmBasePartPriceDO2DTO(t)).collect(Collectors.toList());
        return new PageData<>(((int) page.getTotal()), crmBasePartPriceDTOS);
    }

    @Override
    public PageData<List<CrmBasePartPriceDTO>> selectPageConditionList(CrmBasePartPriceDTO crmBasePartPriceDTO, Integer limit, Integer curr) {
        Page<CrmBasePartPriceDO> page = new Page<>(curr, limit);
        PartModelDO partModel = null;
        PartModelDTO model = crmBasePartPriceDTO.getPartModel();
        if(model != null){
            partModel = PartModelDO.builder()
                    .partTypeUid(CommonUtils.stringNull(model.getPartTypeUid()))
                    .partBrand(CommonUtils.warpStringNull(model.getPartBrand()))
                    .partName(CommonUtils.warpStringNull(model.getPartName()))
                    .partModel(CommonUtils.warpStringNull(model.getPartModel()))
                    .partCode(CommonUtils.warpStringNull(model.getPartCode()))
                    .partSpecification(CommonUtils.warpStringNull(model.getPartSpecification()))
                    .build();
        }
        CrmBaseDO crmBase = null;
        CrmBaseDTO crm = crmBasePartPriceDTO.getCrmBase();
        if (crm != null) {
            crmBase = CrmBaseDO.builder()
                    .crmCode(CommonUtils.warpStringNull(crm.getCrmCode()))
                    .crmName(CommonUtils.warpStringNull(crm.getCrmName()))
                    .crmOtherName(CommonUtils.warpStringNull(crm.getCrmOtherName()))
                    .build();
        }

        CrmBasePartPriceDO crmBasePartPriceDO = CrmBasePartPriceDO.builder()
                .partModelUid(crmBasePartPriceDTO.getPartModelUid())
                .crmBaseUid(crmBasePartPriceDTO.getCrmBaseUid())
                .offerPerson(crmBasePartPriceDTO.getOfferPerson())
                .objuid(crmBasePartPriceDTO.getObjuid())
                .partModel(partModel)
                .crmBase(crmBase)
                .build();
        List<CrmBasePartPriceDO> crmBasePartPriceDOS = crmBasePartPriceMapper.selectPageConditionList(crmBasePartPriceDO, page);
        List<CrmBasePartPriceDTO> crmBasePartPriceDTOS = crmBasePartPriceDOS.stream().map(t -> CustomBeanUtils.crmBasePartPriceDO2DTO(t)).collect(Collectors.toList());
        return new PageData<>(((int) page.getTotal()), crmBasePartPriceDTOS);
    }

    @Override
    public PageData<List<CrmBasePartPriceQueryDTO>> selectPageConditionList(CrmBasePartPriceQueryDTO crmBasePartPriceQueryDTO, Integer limit, Integer curr) {
        Page<CrmBasePartPriceQueryDO> page = new Page<>(curr, limit);
        List<CrmBasePartPriceQueryDO> crmBasePartPriceQueryDOS = crmBasePartPriceQueryMapper.selectPageConditionList(CrmBasePartPriceQueryDO.builder()
                .crmBaseUid(CommonUtils.stringNull(crmBasePartPriceQueryDTO.getCrmBaseUid()))
                .offerPerson(CommonUtils.warpStringNull(crmBasePartPriceQueryDTO.getOfferPerson()))
                .ccontCode(CommonUtils.warpStringNull(crmBasePartPriceQueryDTO.getCcontCode()))
                .fpartModel(CommonUtils.warpStringNull(crmBasePartPriceQueryDTO.getFpartModel()))
                .dquotationCode(CommonUtils.warpStringNull(crmBasePartPriceQueryDTO.getDquotationCode()))
                .eprodCode(CommonUtils.warpStringNull(crmBasePartPriceQueryDTO.getEprodCode()))
                .fpartBrand(CommonUtils.warpStringNull(crmBasePartPriceQueryDTO.getFpartModel()))
                .build(), page);
        List<CrmBasePartPriceQueryDTO> crmBasePartPriceQueryDTOS = crmBasePartPriceQueryDOS.stream().map(CustomBeanUtils::crmBasePartPriceQueryDO2DTO).collect(Collectors.toList());
        return new PageData<>(((int) page.getTotal()), crmBasePartPriceQueryDTOS);
    }
}
